#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

u8 nt37350m_bufferR[54];
u8 nt37350m_bufferG[54];
u8 nt37350m_bufferB[54];

u8 nt37350m_REG_R0[18]; //length+cmd+data
u8 nt37350m_REG_R1[18];
u8 nt37350m_REG_R2[18];
u8 nt37350m_REG_R3[8];

u8 nt37350m_REG_G0[18]; //length+cmd+data
u8 nt37350m_REG_G1[18];
u8 nt37350m_REG_G2[18];
u8 nt37350m_REG_G3[8];

u8 nt37350m_REG_B0[18]; //length+cmd+data
u8 nt37350m_REG_B1[18];
u8 nt37350m_REG_B2[18];
u8 nt37350m_REG_B3[8];


void NT37350M_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u16 tmp;
	u8 tempReg;
	u8 i,j;

	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(5);
	buffer1[0] = 0x02;        
	buffer1[1] = 0xff;
	buffer1[2] = 0x21; //CMD2 Page1
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
			    	
	switch(buffer[4]) //gamma select
	{
		case 1:tempReg=0xB2; break; //gamma1
		case 2:tempReg=0xBF; break; //gamma2
		case 3:tempReg=0xCC; break; //gamma3
		case 4:tempReg=0xDA; break;	//HBM		
		case 5:tempReg=0xE8; break; //IDLE
		default:;break;
	}	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
	if(buffer[3]==0x00)	
	{
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg ,16, nt37350m_bufferR);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+1 ,16, nt37350m_bufferR+16);	delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+2 ,16, nt37350m_bufferR+16+16);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+3 ,6, nt37350m_bufferR+16+16+16);delay_ms(15);
	}
	else if(buffer[3]==0x42)	
	{
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+4 ,16, nt37350m_bufferG);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+5 ,16, nt37350m_bufferG+16);	delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+6 ,16, nt37350m_bufferG+16+16);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+7 ,6, nt37350m_bufferG+16+16+16);delay_ms(15);
	}
	else if(buffer[3]==0x81)
	{
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+8 ,16, nt37350m_bufferB);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+9 ,16, nt37350m_bufferB+16);	delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+10 ,16, nt37350m_bufferB+16+16);delay_ms(15);
		Generate_MIPI_Read(pdev,SigMode,channel,tempReg+11 ,6, nt37350m_bufferB+16+16+16);delay_ms(15);
	}
	
	for(j=0;j<54;j++)
	{
		if(buffer[3]==0x00)				
			buffer[4+j]=nt37350m_bufferR[j];	
		else if(buffer[3]==0x42)	
			buffer[4+j]=nt37350m_bufferG[j];	
		else if(buffer[3]==0x81)
			buffer[4+j]=nt37350m_bufferB[j];	
	}	
			
	buffer[0]=NT37350M;	//
	buffer[1]=0x09;

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  	
}

void NT37350M_Inter_BIST(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	

}

void NT37350M_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

}
void NT37350M_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
 
}


void NT37350M_Video_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[20];
	u8 tempReg,tempPage;
	u8 i,j;
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(5);
	switch(buffer[4]) //gamma select
	{
		case 1: tempReg=0xB2; break; //gamma1
		case 2: tempReg=0xBF; break; //gamma2
		case 3: tempReg=0xCC; break; //gamma3
		case 4: tempReg=0xDA; break;	//HBM		
		case 5: tempReg=0xE8; break; //IDLE
		default:;break;
	}
	
	buffer1[0] = 0x02;        
	buffer1[1] = 0xff;
	buffer1[2] = 0x21; //GAMMA 3 /hbm /
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
	delay_ms(2);
	
	if((buffer[3]&0xFF)==0x00)  //R
	{
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_R0[i]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_R1[i]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_R2[i]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_R3[i]=buffer[i+5+16+16+16];}
  }
	else if((buffer[3]&0xFF)==0x42)  //G
	{
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_G0[i]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_G1[i]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_G2[i]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_G3[i]=buffer[i+5+16+16+16];}
	}
	else if((buffer[3]&0xFF)==0x81)  //B
	{
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_B0[i]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_B1[i]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_B2[i]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_B3[i]=buffer[i+5+16+16+16];}
	}

//------------------------gamma R------------------------------	
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg); //B2
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_R0[i]);	
	}				 
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+1); //B3
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_R1[i]);	
	}				
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+2); //B4
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_R2[i]);	
	}	
	SSD2828_W_Reg(SigMode,channel,0xBC,7); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+3); //B5
	for(i=0; i<6; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_R3[i]);	
	}			
	delay_ms(5); 	
//------------------------gamma G------------------------------			
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+4); //B6
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_G0[i]);	
	}				 
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+5); //B7
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_G1[i]);	
	}				
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+6); //B8
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_G2[i]);	
	}	
	SSD2828_W_Reg(SigMode,channel,0xBC,7); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+7); //B9
	for(i=0; i<6; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_G3[i]);	
	}			
	delay_ms(5); 	
//------------------------gamma B------------------------------			
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+8); //B6
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_B0[i]);	
	}				 
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+9); //B7
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_B1[i]);	
	}				
	SSD2828_W_Reg(SigMode,channel,0xBC,17); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+10); //B8
	for(i=0; i<16; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_B2[i]);	
	}	
	SSD2828_W_Reg(SigMode,channel,0xBC,7); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,tempReg+11); //B9
	for(i=0; i<6; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37350m_REG_B3[i]);	
	}			
	delay_ms(5); 
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}
	
void NT37350M_Command_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
	u8 i,j,k=0,temp;
	u8 tempReg;   	
	switch(buffer[4]) //gamma select
	{
		case 1: tempReg=0xB2; break; //gamma1
		case 2: tempReg=0xBF; break; //gamma2
		case 3: tempReg=0xCC; break; //gamma3
		case 4: tempReg=0xDA; break;	//HBM		
		case 5: tempReg=0xE8; break; //IDLE
		default:;break;
	}
	if((buffer[3]&0xFF)==0x00)  //R
	{
		nt37350m_REG_R0[0]=17;
		nt37350m_REG_R0[1]=tempReg;
		nt37350m_REG_R1[0]=17;
		nt37350m_REG_R1[1]=tempReg+1;
		nt37350m_REG_R2[0]=17;
		nt37350m_REG_R2[1]=tempReg+2;
		nt37350m_REG_R3[0]=7;
		nt37350m_REG_R3[1]=tempReg+3;
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_R0[i+2]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_R1[i+2]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_R2[i+2]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_R3[i+2]=buffer[i+5+16+16+16];}
	}
	else if((buffer[3]&0xFF)==0x42)  //G
	{
		nt37350m_REG_G0[0]=17;
		nt37350m_REG_G0[1]=tempReg+4;
		nt37350m_REG_G1[0]=17;
		nt37350m_REG_G1[1]=tempReg+5;
		nt37350m_REG_G2[0]=17;
		nt37350m_REG_G2[1]=tempReg+6;
		nt37350m_REG_G3[0]=7;
		nt37350m_REG_G3[1]=tempReg+7;
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_G0[i+2]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_G1[i+2]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_G2[i+2]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_G3[i+2]=buffer[i+5+16+16+16];}
	}
	else if((buffer[3]&0xFF)==0x81)  //B
	{
		nt37350m_REG_B0[0]=17;
		nt37350m_REG_B0[1]=tempReg+8;
		nt37350m_REG_B1[0]=17;
		nt37350m_REG_B1[1]=tempReg+9;
		nt37350m_REG_B2[0]=17;
		nt37350m_REG_B2[1]=tempReg+10;
		nt37350m_REG_B3[0]=7;
		nt37350m_REG_B3[1]=tempReg+11;
		for( i = 0;i<16;i++)   //8个绑点                                                                                  
		{nt37350m_REG_B0[i+2]=buffer[i+5];}	    
		for( i = 0;i<16;i++)   //8个绑点                                                                                 
		{nt37350m_REG_B1[i+2]=buffer[i+5+16];}
		for( i = 0;i<16;i++)   //8个绑点	                                                                                  
		{nt37350m_REG_B2[i+2]=buffer[i+5+16+16];}
		for( i = 0;i<6;i++)   //3个绑点	                                                                                  
		{nt37350m_REG_B3[i+2]=buffer[i+5+16+16+16];}
	}

	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data); //LP mode only
	delay_ms(5);	
	
	buffer1[0] = 0x02;        
	buffer1[1] = 0xff;
	buffer1[2] = 0x21; //GAMMA 3 /hbm /
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
	delay_ms(2);			
	//GAMMA RED GROUP1
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_R0,0);
	//GAMMA RED GROUP2
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_R1,0);
	//GAMMA RED GROUP3
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_R2,0);
	//GAMMA RED GROUP4
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_R3,0);
//---------------------------------------------------------------------		
	//GAMMA RED GROUP1
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_G0,0);
	//GAMMA RED GROUP2
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_G1,0);
	//GAMMA RED GROUP3
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_G2,0);		
	//GAMMA RED GROUP4
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_G3,0);	
//----------------------------------------------------------------------		
	//GAMMA RED GROUP1
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_B0,0);
	//GAMMA RED GROUP2
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_B1,0);
	//GAMMA RED GROUP3
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_B2,0);
	//GAMMA RED GROUP4
	SSD2828_W_Array(SigMode,channel,nt37350m_REG_B3,0);		
//---------------------------------------------------------		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
	delay_ms(2); 
    buffer[0]=0x75;	//NT37350
    buffer[1]=0x08;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
}


//write  gamma
void NT37350M_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	if(SigMode==Video_Mode)
	{
		NT37350M_Video_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	}
	else if(SigMode==CMD_Mode)
	{
		NT37350M_Command_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	}
}

void NT37350M_Gamma_Update(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
}


void NT37350M_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
}


void ProcessForIc75( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(buffer[1])
	{
		case 0x01:                                      //写寄存器
			 NT37350M_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
		case 0x02:                                      //读寄存器
			 NT37350M_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
		case 0x03:                                     //自动gamma写
			 NT37350M_Gamma_Update(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;			
		case 0x08:                                      //写Gamma数据    
			 NT37350M_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x09:                                      //读Gamma数据
			 NT37350M_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x0D://OTP Gamma
			 NT37350M_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);  break;

    default:	break;	
	}	
}


